<?php

namespace App\Http\Controllers;

use App\Mail\UserMail;
use App\Models\Blogs;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;

class UserController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth', [
            'except' => ['index', 'show', 'create', 'store', 'emailToken']
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::orderBy('id', 'desc')->simplePaginate(6);
        return view('user.index', compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function create()
    {
        return view('user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param        \Illuminate\Http\Request  $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $this->validate($request, [
            'name' => 'required|min:2|max:20',
            'email' => 'required|min:2|email|unique:users',
            'password' => 'required|min:5|confirmed',
        ]);
        $data['password'] = bcrypt($request->password);
        $data['email_token'] = Str::random(10);
        $user = User::create($data);
        session()->flash('success', '注册成功,请前往邮箱进行认证');
        Mail::to($user)->send(new UserMail($user->email_token));
        return redirect()->route('home');
    }

    /**
     * Display the specified resource.
     *
     * @param        \App\Models\User  $user
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function show(User $user)
    {
        $blogs = Blogs::query()->where('user_id', $user->id)->paginate(3);
        if (Auth::check()) {
            $follow = $user->isfollowing(Auth::user()->id) ? '取消关注' : '关注';
        } else {
            $follow = '关注';
        }

        return view('user.show', compact('user', 'blogs', 'follow'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param        \App\Models\User  $user
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        return view('user.edit', compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param        \Illuminate\Http\Request  $request
     * @param        \App\Models\User  $user
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
     */
    public function update(Request $request, User $user)
    {
        $this->validate($request, [
            'password' => 'required|min:5|confirmed'
        ]);

        $user->password = bcrypt($request->password);
        if ($user->save()) {
            session()->flash('success', '修改成功！');
            return redirect()->route('home');
        }
        session()->flash('danger', '修改失败！');
        return redirect()->route('user.edit');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param        \App\Models\User  $user
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        $this->authorize('delete', $user);
        $user->delete();
        session()->flash('success', '删除成功！');
        return redirect()->route('user.index');
    }

    //邮箱验证
    public function emailToken(Request $request)
    {
        $user = User::where('email_token', $request->token)->first();
        $user->active = true;
        $user->save();
        session()->flash('success', '验证成功');
        return redirect()->route('loginshow');
    }

    public function follow(User $user)
    {
        if (Auth::check()) {
            $user->followToggle(Auth::user()->id);
        }
        return back();
    }
}
